 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-9-1  <br>
 */
package com.ce.pms.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.common.util.StringAnalysiUtil;
import com.ce.pms.customer.dao.IClientDao;
import com.ce.pms.customer.dao.IClientRHouseDao;
import com.ce.pms.customer.model.ClientDO;
import com.ce.pms.customer.model.ClientRHouseDO;
import com.ce.pms.customer.model.ParticipantDO;
import com.ce.pms.customer.service.IClientRHouseService;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.dao.IHouseDao;
import com.ce.util.BlankUtil;

/**
 * 	名称: com.ce.pms.customer.service.impl.ClientRHouseServiceImpl <br>
 *
 *
 * @author  付湘辉  <br>
 * Created on： 2008-8-15 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class ClientRHouseServiceImpl extends PmsBaseServiceImpl implements
		IClientRHouseService {

	private IClientRHouseDao clientRHouseDao;

	protected IHouseDao houseDao;

	protected IClientDao clientDao;

	/**
	 * 功能描述: 根据房间id得到所有的业主实体 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<ParticipantDO> getAllOwnerByHouseId(Long houseId){
		super.checkParameterIsNull("common:parameters_null_exception,ID", houseId);
		SysrefcodeDO clientType = this.sysrefcodeDao.getSysrefCodesByCode(SysrefcodeDO.CS_CLIENT_CLIENT_TYPE_CODE, SysrefcodeDO.CLIENT_TYPE_OWNER);
		List<ClientRHouseDO> list = this.clientRHouseDao.getClientRHouseListByHouseIdAndClientType(houseId, clientType.getId());

		List<ParticipantDO> resList = new ArrayList<ParticipantDO>();
		if (!BlankUtil.isBlank(list)) {
			for (ClientRHouseDO cRhouseDo : list) {
				resList.add(cRhouseDo.getClient().getParticipant());
			}
		}
		return resList;
	}

	/**
	 * 功能描述: 通过房屋id和客户类型id得到其对应的客户房屋关系列表  <br>
	 * @param houseIds
	 * @param clientTypeId
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-11-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<ClientRHouseDO> getClientRHouseListByHouseIdAndClientType(Long houseId,Long clientId){
		checkParameterIsNull("common:parameters_null_exception, ", houseId);
		checkParameterIsNull("common:parameters_null_exception, ", clientId);

		String houseIds = houseDao.getHouseIdsById(houseId);
		ClientDO client = this.clientDao.findById(clientId);
		if(client == null) //没有找到对应的客户
			throw new BusinessException("customer:customer_service_movein_validation_clientIsNull",BusinessException.ERROR);

		Long clientTypeId = client.getClientType().getId(); //获取客户对应的客户类型ID

		List<ClientRHouseDO> clientRHouselist  = clientRHouseDao.getClientRHouseListByHouseIdAndClientType(houseIds,clientTypeId);
		return clientRHouselist;
	}
	/**
	 * 功能描述: 根据房间id得到所有的业主名称列表 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<String> getAllOwnerNameByHouseId(Long houseId){
		super.checkParameterIsNull("common:parameters_null_exception,iD", houseId);
		List<ParticipantDO> pList = this.getAllOwnerByHouseId(houseId);
		List<String> resList = new ArrayList<String>();
		if (!BlankUtil.isBlank(pList)) {
			for (ParticipantDO participantDO : pList) {
				resList.add(participantDO.getParticipantName());
			}
		}
		return resList;
	}

	/**
	 * 功能描述: 根据房间id得到所有的业主名称，用逗号分隔 <br>
	 * @param houseId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-15  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String getAllOwnerNameJoinByHouseId(Long houseId){
		super.checkParameterIsNull("common:parameters_null_exception,iD", houseId);
		List<String> resList = this.getAllOwnerNameByHouseId(houseId);

		String[] strs=new String[resList.size()];

		return StringAnalysiUtil.portfolioFormattedString(resList.toArray(strs));
	}

	/**
	 * @param clientRHouseDao the clientRHouseDao to set
	 */
	public void setClientRHouseDao(IClientRHouseDao clientRHouseDao) {
		this.clientRHouseDao = clientRHouseDao;
	}

	/**
	 * 功能描述: 根据房屋id和参与人id查询相应的客户房屋关系实体,
	 * 如果搬入日期不为空而搬出日期为空则返回查询结果，否则返回空值。 <br>
	 * @param houseId
	 * @param participantId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-11-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public ClientRHouseDO getCRHByHouseAndParticipantId(Long houseId,Long participantId){
		super.checkParameterIsNull("common:parameters_null_exception,ID", houseId,participantId);
		ClientRHouseDO clientRHouseDO= this.clientRHouseDao.getClientRHouseByHouseAndParticipantId(houseId,participantId);
		if (!BlankUtil.isBlank(clientRHouseDO)) {
			if (clientRHouseDO.getInDate()!=null && clientRHouseDO.getOutDate()==null) {
				return clientRHouseDO;
			}
		}
		return null;
	}

	public void setHouseDao(IHouseDao houseDao) {
		this.houseDao = houseDao;
	}

	public void setClientDao(IClientDao clientDao) {
		this.clientDao = clientDao;
	}
}
